package com.hsgene.goods.dto;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.hsgene.common.util.basic.JsonUtils;
import com.hsgene.common.util.date.TimeJsonDeserializer;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.goods.constants.DetectionEnum;
import com.hsgene.goods.constants.GoodsMallStatus;
import com.hsgene.goods.domain.GoodsMall;
import com.hsgene.goods.dto.specification.GoodsSpecificationDto;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商城商品信息Dto
 *
 * @author wangbing
 * @version 1.0, 2018/7/14
 */
public class GoodsMallDto implements Serializable {

    private static final long serialVersionUID = -721535043148964458L;
    /**
     * 商城商品编号
     */
    private String id;

    /**
     * 商品编号
     */
    private String goodsId;

    /**
     * 发布平台：1 = WEB，2 = APP，3 = 小程序
     */
    private Byte platformType;

    /**
     * 主分类
     */
    private Integer mainCategory;

    /**
     * 主分类名字
     */
    private String mainCategoryName;

    /**
     * 子分类
     */
    private Integer subCategory;

    /**
     * 子分类名字
     */
    private String subCategoryName;

    /**
     * 供应商名称
     */
    private String supplierName;

    /**
     * 套餐名称
     */
    private String goodsName;

    /**
     * 封面图
     */
    private String coverUrl;

    /**
     * 套餐商品banner介绍
     */
    private List<GoodsBannerDto> goodsBanners;

    /**
     * 市场价
     */
    private BigDecimal marketPrice;

    /**
     * 结算价格
     */
    private BigDecimal settlementPrice;

    /**
     * 价格区间开始
     */
    private BigDecimal priceSectionStart;

    /**
     * 价格区间结束
     */
    private BigDecimal priceSectionEnd;

    /**
     * 检测时长
     */
    private Short detectionDuration;

    /**
     * 库存数量
     */
    private Integer inventoryCount;

    /**
     * 规格包装
     */
    private List<GoodsSpecificationDto> specification;

    /**
     * 产品介绍（富文本）
     */
    private String presentation;

    /**
     * 状态：待完善、待发布、已发布、修改申请中、上架中、已下架
     */
    private Integer status;

    /**
     * 状态描述
     */
    private String statusDesc;

    /**
     * 商品检测相关信息Map（key：detection_type = 检测类型：肿瘤，detection_locus = 检测位点（如：EGFR），sample_mode = 采样方式（如：唾液、血液、蜡片），detection_mode = 检测方式（如：芯片、二代测序））
     */
    private Map<String, List<GoodsDetectionDto>> detectionMap;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = TimeJsonDeserializer.class)
    private Date createTime;

    /**
     * 更新时间
     */
    @JsonDeserialize(using = TimeJsonDeserializer.class)
    private Date updateTime;

    /**
     * 商品快照id
     */
    private String snapshootId;

    public GoodsMallDto() {
    }

    public GoodsMallDto(GoodsMall goodsMall) {
        this.id = goodsMall.getId();
        this.goodsId = goodsMall.getGoodsId();
        this.platformType = goodsMall.getPlatformType();
        this.mainCategory = goodsMall.getMainCategory();
        this.subCategory = goodsMall.getSubCategory();
        this.supplierName = goodsMall.getSupplierName();
        this.goodsName = goodsMall.getGoodsName();
        this.coverUrl = goodsMall.getCoverUrl();
        if (StringUtils.isNotBlank(goodsMall.getGoodsBanner())) {
            this.goodsBanners = JsonUtils.jsonToObject(goodsMall.getGoodsBanner(), new TypeReference<List<GoodsBannerDto>>() {
            });
        }
        this.marketPrice = goodsMall.getMarketPrice();
        this.settlementPrice = goodsMall.getSettlementPrice();
        this.priceSectionStart = goodsMall.getPriceSectionStart();
        this.priceSectionEnd = goodsMall.getPriceSectionEnd();
        this.detectionDuration = goodsMall.getDetectionDuration();
        this.inventoryCount = goodsMall.getInventoryCount();
        if (StringUtils.isNotBlank(goodsMall.getSpecification())) {
            try {
                this.specification = JsonUtils.jsonToObject(goodsMall.getSpecification(), new TypeReference<List<GoodsSpecificationDto>>() {
                });
            } catch (Exception e) {
                this.specification = null;
            }
        }

        this.presentation = goodsMall.getPresentation();
        this.status = goodsMall.getStatus();
        if (this.status != null) {
            this.statusDesc = GoodsMallStatus.getEnum(this.status).getDesc();
        }
        this.createTime = goodsMall.getCreateTime();
        this.updateTime = goodsMall.getUpdateTime();
        this.snapshootId = goodsMall.getSnapshootId();
    }

    /**
     * 商品信息字段校验
     */
    public void check() {
        FieldChecker.checkEmpty(this.mainCategory, "商品主分类");
        FieldChecker.checkEmpty(this.subCategory, "商品子分类");
        FieldChecker.checkEmpty(this.goodsName, "套餐名称");
        FieldChecker.checkEmpty(this.coverUrl, "套餐封面图");
        FieldChecker.checkEmpty(this.marketPrice, "市场价");
        FieldChecker.checkEmpty(this.settlementPrice, "结算价格");
        FieldChecker.checkEmpty(this.priceSectionStart, "价格区间开始");
        FieldChecker.checkEmpty(this.priceSectionEnd, "价格区间结束");
        FieldChecker.checkEmpty(this.detectionDuration, "工作日");
        FieldChecker.checkEmpty(this.inventoryCount, "库存数量");
        FieldChecker.checkEmpty(this.specification, "规格包装");
        FieldChecker.checkEmpty(this.presentation, "产品介绍");
        FieldChecker.checkEmpty(this.detectionMap, "检测类型");
        FieldChecker.checkEmpty(
                this.detectionMap.get(DetectionEnum.DETECTION_TYPE.getValue()), "检测类型");
        FieldChecker.checkEmpty(
                this.detectionMap.get(DetectionEnum.DETECTION_LOCUS.getValue()), "检测位点");
        FieldChecker.checkEmpty(
                this.detectionMap.get(DetectionEnum.SAMPLE_MODE.getValue()), "采样方式");
        FieldChecker.checkEmpty(
                this.detectionMap.get(DetectionEnum.DETECTION_MODE.getValue()), "检测方式");
        FieldChecker.checkEmpty(this.getPresentation(), "产品介绍");
    }

    public String getMainCategoryName() {
        return mainCategoryName;
    }

    public void setMainCategoryName(String mainCategoryName) {
        this.mainCategoryName = mainCategoryName;
    }

    public String getSubCategoryName() {
        return subCategoryName;
    }

    public void setSubCategoryName(String subCategoryName) {
        this.subCategoryName = subCategoryName;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public Byte getPlatformType() {
        return platformType;
    }

    public void setPlatformType(Byte platformType) {
        this.platformType = platformType;
    }

    public Integer getMainCategory() {
        return mainCategory;
    }

    public void setMainCategory(Integer mainCategory) {
        this.mainCategory = mainCategory;
    }

    public Integer getSubCategory() {
        return subCategory;
    }

    public void setSubCategory(Integer subCategory) {
        this.subCategory = subCategory;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public List<GoodsBannerDto> getGoodsBanners() {
        return goodsBanners;
    }

    public void setGoodsBanners(List<GoodsBannerDto> goodsBanners) {
        this.goodsBanners = goodsBanners;
    }

    public BigDecimal getSettlementPrice() {
        return settlementPrice;
    }

    public void setSettlementPrice(BigDecimal settlementPrice) {
        this.settlementPrice = settlementPrice;
    }

    public BigDecimal getPriceSectionStart() {
        return priceSectionStart;
    }

    public void setPriceSectionStart(BigDecimal priceSectionStart) {
        this.priceSectionStart = priceSectionStart;
    }

    public BigDecimal getPriceSectionEnd() {
        return priceSectionEnd;
    }

    public void setPriceSectionEnd(BigDecimal priceSectionEnd) {
        this.priceSectionEnd = priceSectionEnd;
    }

    public Short getDetectionDuration() {
        return detectionDuration;
    }

    public void setDetectionDuration(Short detectionDuration) {
        this.detectionDuration = detectionDuration;
    }

    public Integer getInventoryCount() {
        return inventoryCount;
    }

    public void setInventoryCount(Integer inventoryCount) {
        this.inventoryCount = inventoryCount;
    }

    public List<GoodsSpecificationDto> getSpecification() {
        return specification;
    }

    public void setSpecification(List<GoodsSpecificationDto> specification) {
        this.specification = specification;
    }

    public String getPresentation() {
        return presentation;
    }

    public void setPresentation(String presentation) {
        this.presentation = presentation;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Map<String, List<GoodsDetectionDto>> getDetectionMap() {
        return detectionMap;
    }

    public void setDetectionMap(Map<String, List<GoodsDetectionDto>> detectionMap) {
        this.detectionMap = detectionMap;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getSnapshootId() {
        return snapshootId;
    }

    public void setSnapshootId(String snapshootId) {
        this.snapshootId = snapshootId;
    }

    public BigDecimal getMarketPrice() {
        return marketPrice;
    }

    public void setMarketPrice(BigDecimal marketPrice) {
        this.marketPrice = marketPrice;
    }
}
